package com.ydj.framework.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {
    /**
     * 获取一段 N 位的随机码
     *
     * @param size  随机码的位数
     * @return      随机码
     */
    public static String getRandomCode(int size) {
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取一个N位验证码
     * @return 验证码
     */
    public static String generateVerifyCode(int length) {
        // 确保位数合法（至少1位）
        if (length <= 0) {
            throw new IllegalArgumentException("验证码长度必须大于0");
        }
        // 生成最小值和最大值
        int min = (int) Math.pow(10, length - 1);
        int max = (int) Math.pow(10, length) - 1;

        // 在范围内随机
        int verifyCode = min + (int) (Math.random() * (max - min + 1));

        return String.valueOf(verifyCode);
    }

    /**
     * 是否中文
     *
     * @param str
     * @return
     */
    public static Boolean isChinese(String str) {
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
        Matcher matcher = pattern.matcher((str));
        return matcher.find();
    }

    /**
     * 根据Session获取token
     *
     * @return token
     */
    public static String getToken() {
        return String.format("%s-%s", UUID.randomUUID(), getRandomCode(8));
    }

    public static String hidePhone(String phone) {
        if (StringUtils.isNoneBlank(phone)) {
            return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
        return null;
    }

	public static Long converAmount(String amount){
		return new BigDecimal(amount).multiply(new BigDecimal("100")).longValue();
	}

    public static String converAmount(Long amount) {
        DecimalFormat f = new DecimalFormat("0.00");
        return f.format(amount.doubleValue() / 100);
    }

    public static String ignoreCharacter(String str) {
        int length = 30;
        if (str.length() > length) {
            str = str.substring(0, length) + "....";
        }
        return str;
    }

    public static Long changeObjectToLong(Object object) {
        if (object instanceof String) {
            return Long.parseLong((String) object);
        }
        return (Long) object;
    }

    /**
     * 获取最后4位字符
     *
     * @param str
     * @return
     */
    public static String getLast4Char(String str) {
        int length = str.length();
        int l = 4;
        if (str.length() > length) {
            str = str.substring(length - l, length);
        }
        return str;
    }

    /**
     * 将文件转化为字节数据
     *
     * @param file 文件
     * @return 结果
     */
    public static byte[] fileChangeByteArray(File file) throws Exception {
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        return bos.toByteArray();
    }
}